package com.sczmgk.hotline_pzh.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sczmgk.hotline_pzh.MyApp;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.BaseActivity;
import com.sczmgk.hotline_pzh.callback.SimplePaserJsonCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.entity.ComplainInfo;
import com.sczmgk.hotline_pzh.entity.ComplaintStepBean;
import com.sczmgk.hotline_pzh.entity.StepInfo;
import com.sczmgk.hotline_pzh.httputils.MyHttp;
import com.sczmgk.hotline_pzh.sp.CacheUtils;
import com.sczmgk.hotline_pzh.utils.CommonUtils;
import com.sczmgk.hotline_pzh.utils.MyLog;
import com.sczmgk.hotline_pzh.utils.Tools;
import com.sczmgk.hotline_pzh.view.MyListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sczmgk.hotline_pzh.constants.Constants.STATUS_CANCEL;
import static com.sczmgk.hotline_pzh.constants.Constants.STATUS_EVALUTED;
import static com.sczmgk.hotline_pzh.constants.Constants.STATUS_PRETREATMENTED;
import static com.sczmgk.hotline_pzh.constants.Constants.STATUS_PRETREATMENT_FAIL;
import static com.sczmgk.hotline_pzh.constants.Constants.STATUS_PROCESSING;
import static com.sczmgk.hotline_pzh.constants.Constants.STATUS_WAITING_CHECK;
import static com.sczmgk.hotline_pzh.constants.Constants.STATUS_WAITING_EVALUTE;
import static com.sczmgk.hotline_pzh.constants.Constants.STATUS_WAITING_PRETREATMENT;
import static com.sczmgk.hotline_pzh.constants.Constants.STATUS_WAITING_PROCESS;


/**
 * Created by Administrator on 2016/11/28 0028.
 */
public class ComplaintDetailActivity extends BaseActivity {

    /*投诉处理的标题,投诉编号,投诉事件*/
    private TextView mComplaintDetailTitle;
    private TextView mComplaintDetailNumber;
    private TextView mComplaintDetailDate;

    /*更多详情的按钮*/
    private Button mBtnComplaintDetailMoreInfo;

    /*投诉处理的信息*/
    private ComplainInfo complaintInfo;

    /*步骤的ListView和Adapter*/
    private MyAdapter mAdapter;
    private MyListView mStepListView;

    private static final int REFRESH_LIST = 0;

    /*步骤的标识*/
    private static final int TREATMENT_STEP_ONE = 1;
    private static final int TREATMENT_STEP_TWO = 2;
    private static final int TREATMENT_STEP_THREE = 3;

    /*时光轴的集合*/
    private List<StepInfo> steps;
    /**
     * 查看处理结果
     */
    private Button mResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaint_detail);

        //将activity添加到集合中
        CommonUtils.addActivity(this);

        setHeader();

        initData();

        initView();


        setListener();
    }

    /**
     * 设置标题栏
     */
    private void setHeader() {
        setTitle(R.string.complaint_detail_header_title);
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化控件
     */
    private void initView() {

        //投诉详情的标题,编号,时间
        mComplaintDetailTitle = (TextView) findViewById(R.id.tv_complaint_detail_title);
        mComplaintDetailNumber = (TextView) findViewById(R.id.tv_complaint_detail_number);
        mComplaintDetailDate = (TextView) findViewById(R.id.tv_complaint_detail_date);

        //详情更多的按钮
        mBtnComplaintDetailMoreInfo = (Button) findViewById(R.id.btn_complaint_detail_more_info);

        //进度追踪的ListView
        mStepListView = (MyListView) findViewById(R.id.lv_step);
        mResult = (Button) findViewById(R.id.btn_complaint_result);
        if (complaintInfo.status > STATUS_PROCESSING && complaintInfo.status < STATUS_CANCEL) {
            mResult.setVisibility(View.VISIBLE);
        } else {
            mResult.setVisibility(View.GONE);
        }

        mComplaintDetailTitle.setText(complaintInfo.title);
        mComplaintDetailNumber.setText(complaintInfo.id);
        mComplaintDetailDate.setText(complaintInfo.createtime);
    }

    /**
     * 初始化数据
     */
    private void initData() {
        //创建步骤的集合
        steps = new ArrayList<>();
        //获取投诉的信息
        Intent intent = getIntent();
        complaintInfo = (ComplainInfo) intent.getSerializableExtra(Constants.COMPLAIN_INFO_KEY);
        //设置标题,编号,时间

        //访问网络获取步骤详情
        Map<String, Object> infos = new HashMap<>();
        infos.put("userid", CacheUtils.getIntData(ComplaintDetailActivity.this, Constants.LOGIN_UID, 0));
        infos.put("id", complaintInfo.id);
        infos.put("imei", MyApp.imei);

        MyHttp.requestByCommon(Constants.BASE_URL + Constants.COMPLAINT_SETTING_DETAIL_STEP_URL, infos, MyHttp.GET, this, false, null, new SimplePaserJsonCallback() {

            @Override
            public void onVerifyIsNull() {

            }

            @Override
            public void onVerifySuccess(String dataRaw, boolean isCache) {
                try {
                    paserSetpJson(dataRaw, isCache);
                } catch (Exception e) {
                    MyLog.e(TAG, "解析json错误");
                    e.printStackTrace();
                }
            }
        });

    }

    /**
     * 处理时光轴的Json数据
     *
     * @param dataRaw
     * @param isCache
     */
    private void paserSetpJson(String dataRaw, boolean isCache) {
        if (Tools.isEmpty(dataRaw)) {
            MyLog.i("获取步骤数据", "get step json error");
            return;
        }

        //解析进度追踪数据并添加到集合中
        Gson gson = new Gson();
        ComplaintStepBean complaintStepBean = gson.fromJson(dataRaw, ComplaintStepBean.class);
        steps.addAll(complaintStepBean.datalist);

        //当Adapter为空时创建Adapter并设置,不为空时发送消息更新Ui
        if (mAdapter == null) {
            mAdapter = new MyAdapter();
            mStepListView.setAdapter(mAdapter);
        } else {
            Message msg = Message.obtain();
            msg.what = REFRESH_LIST;
            mHandler.sendMessage(msg);
        }
    }

    /**
     * 进度追踪ListView的Adapter
     */
    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return steps.size();
        }

        @Override
        public Object getItem(int position) {
            return steps.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder;
            if (convertView == null) {

                viewHolder = new ViewHolder();
                convertView = View.inflate(ComplaintDetailActivity.this, R.layout.item_step_complaint, null);

                //步骤红点的上方线条
                viewHolder.upLine = convertView.findViewById(R.id.tv_step_up_line);
                //步骤红点的下方线条
                viewHolder.downLine = convertView.findViewById(R.id.tv_step_down_line);
                //处理意见
                viewHolder.advice = (TextView) convertView.findViewById(R.id.tv_complaint_step_advice);
                //处理方案
                viewHolder.scheme = (TextView) convertView.findViewById(R.id.tv_complaint_step_scheme);
                //处理时间
                viewHolder.time = (TextView) convertView.findViewById(R.id.tv_complaint_step_time);
                //详情按钮
                viewHolder.tvDetail = (TextView) convertView.findViewById(R.id.tv_complaint_step);
                //步骤红点
                viewHolder.point = (ImageView) convertView.findViewById(R.id.iv_point);
                //底部线条
                viewHolder.line = (TextView) convertView.findViewById(R.id.tv_divider_line);

                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            final StepInfo currentStepInfo = steps.get(position);

            //最后一条隐藏掉下面的分隔线和步骤红点下方的线条
            if (position == steps.size() - 1) {
                viewHolder.line.setVisibility(View.GONE);
                viewHolder.downLine.setVisibility(View.INVISIBLE);
            } else {
                viewHolder.line.setVisibility(View.VISIBLE);
                viewHolder.downLine.setVisibility(View.VISIBLE);
            }

            //改变第一条显示的颜色
            if (position == 0) {

                //如果第一条显示是正在预审,则显示成红色的图片,否则设置成正在处理的图片
                if (currentStepInfo.stepid == TREATMENT_STEP_TWO) {
                    viewHolder.point.setBackgroundResource(R.drawable.point_exception);//设置成红色的图片
                } else {
                    viewHolder.point.setBackgroundResource(R.drawable.point_handling);//设置成正在处理中
                }

                //设置红点上方线条不显示
                viewHolder.upLine.setVisibility(View.INVISIBLE);

                //设置第一条文本显示颜色
                viewHolder.advice.setTextColor(getResources().getColor(R.color.indicator_no_select_color));
                viewHolder.scheme.setTextColor(getResources().getColor(R.color.indicator_no_select_color));
                viewHolder.time.setTextColor(getResources().getColor(R.color.indicator_no_select_color));

            } else {

                //设置红点上方线条显示
                viewHolder.upLine.setVisibility(View.VISIBLE);
                //设置点为灰色点
                viewHolder.point.setBackgroundResource(R.drawable.point_finish);

                //设置文本显示颜色
                viewHolder.advice.setTextColor(getResources().getColor(R.color.common_grey_text_color));
                viewHolder.scheme.setTextColor(getResources().getColor(R.color.common_grey_text_color));
                viewHolder.time.setTextColor(getResources().getColor(R.color.common_grey_text_color));
            }

            //TODO 如果是处理完成就设置详情按钮为显示
            viewHolder.tvDetail.setVisibility(View.GONE);

//            if (position == 0) {
//
//            } else {
//                viewHolder.tvDetail.setVisibility(View.GONE);
//            }

            //步骤详情
            if (currentStepInfo.type == 1) {
                viewHolder.tvDetail.setVisibility(View.VISIBLE);
                //详情的点击事件
                viewHolder.tvDetail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //跳转到步骤详情处理意见的界面
//                         UIUtils.startActivity(ComplaintDetailActivity.this, StepDetailActivity.class, Constants.COMPLAINT_SETTING_LIST_DETAIL, currentStepInfo.stepid);
                        Intent intent = new Intent(ComplaintDetailActivity.this, StepDetailActivity.class);
                        intent.putExtra(Constants.COMOLAIN_STEP_INFO_KEY, currentStepInfo);
                        intent.putExtra(Constants.COMPLAIN_INFO_KEY, complaintInfo);
                        startActivity(intent);

                    }
                });
            }

            //设置处理意见,处理方案以及时间
            viewHolder.advice.setText(currentStepInfo.stepname);
            viewHolder.scheme.setText(currentStepInfo.remark);
            viewHolder.time.setText(currentStepInfo.time);

            return convertView;
        }
    }

    public class ViewHolder {
        //处理意见
        TextView advice;
        //处理方案
        TextView scheme;
        //处理时间
        TextView time;

        //步骤点
        ImageView point;
        //步骤上方线条
        View upLine;
        //步骤下方线条
        View downLine;
        //文本底部线条
        TextView line;
        TextView tvDetail;
    }

    /**
     * 设置监听
     */
    private void setListener() {

        //点击更多按钮的监听
        mBtnComplaintDetailMoreInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //跳转到更多信息的页面
                Intent intent = new Intent(ComplaintDetailActivity.this, ComplaintDetailMoreInfomationActivity.class);
                intent.putExtra(Constants.COMPLAIN_INFO_KEY, complaintInfo);
                startActivity(intent);
            }
        });
        mResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //跳转到步骤详情处理意见的界面

                Intent intent = new Intent(ComplaintDetailActivity.this, OpinionActivity.class);
                intent.putExtra(Constants.COMPLAIN_INFO_KEY, complaintInfo);
                startActivity(intent);
            }
        });
    }

    @Override
    public void handleMessage(Message msg) {

        switch (msg.what) {
            case REFRESH_LIST:
                mAdapter.notifyDataSetChanged();
                break;
        }

    }

    @Override
    public boolean getIsNeedLogin() {
        return true;
    }
}
